<?php namespace Common\Model;
use Hdphp\Model\Model;
/**
 * 分类模型
 */
 class Category extends Model
 {
 	protected $table = "category";
	//自动完成
	protected $validate = array(
	   array('category_name','required','请输入分类名称',3,3),
	   array('category_sort','required','请输入分类排序',3,3),
	);
	/**
	 * 添加
	 */
	public function store()
	{
		if(!$this->create()) return false;
		//执行添加
		return $this->add();
	}
	/**
	 * 获得除了自己以及自己的所有子集数据
	 */
	public function getCateData($cid)
	{
		$cids =$this->getSon($this->get(),$cid);
		$cids[] = $cid;
		$cids = implode(',', $cids);
		$data = $this->where("category_id not in ($cids)")->get();
		//变成树状结构
		return Data::tree($data,"category_name","category_id","category_pid");
	}
	/**
	 * 找子集
	 */
	public function getson($data,$cid)
	{
		//静态变量,为了结果不被覆盖，不丢失
		static $temp = array();
		foreach($data as $k => $v)
		{
			if($cid==$v['category_pid'])
			{
				$temp[] =$v['category_id'];
				$this->getson($data, $v['category_id']); 
			}
		}
		return $temp;
	}
	/**
	 * 编辑
	 */
	public function edit()
	{
		if(!$this->create()) return false;
		//执行修改
		return $this->save();
	}
	
 }
